/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package projekt.zaliczeniowy.tkunit.runner.EndOfTestsObserver;

import java.util.LinkedList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import projekt.zaliczeniowy.tkunit.test.results.TestResult;
import projekt.zaliczeniowy.tkunit.test.results.visitor.TestResultVisitor;
import projekt.zaliczeniowy.tkunit.test.results.visitor.TestResultVisitorPrintingSummary;
import projekt.zaliczeniowy.tkunit.test.results.visitor.TestResultVisitorSummarizingResults;

/**
 * This observer/listener class uses Visitor to get some statistics based on
 * result returned by test runner
 *
 * @author Tomek
 */
public class EndOfTestsObserver implements Observer {

    /**
     * we have even chain of responsibility!! - formally whe should implement
     * passing method in the observer itself but this is fairly easy.
     *
     * but wait - now it's something like a mixture of the chain pattern and
     * mediator
     */
    final private List<TestResultVisitor> chainOfResponsibility;

    public EndOfTestsObserver() {
        chainOfResponsibility = new LinkedList<TestResultVisitor>();
        chainOfResponsibility.add(new TestResultVisitorSummarizingResults());
        chainOfResponsibility.add(new TestResultVisitorPrintingSummary(System.out));
    }

    public EndOfTestsObserver(List<TestResultVisitor> chainOfResp) {
        this.chainOfResponsibility = chainOfResp;
    }

    /**
     * this method uses visitor (one of two in the project to present the
     * results)
     *
     * @param o in fact of type TestRunner
     * @param arg in fact of type List<TestResult>
     */
    public void update(Observable o, Object arg) {
        List<TestResult> testResults = (List<TestResult>) arg;
        for (TestResult tr : testResults) {
            for (TestResultVisitor visitor : chainOfResponsibility) {
                visitor.visit(tr);
            }
        }
    }
}
